package com.cincc.general;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cincc.general.common.AttrsConstant;
import com.cincc.general.common.SharePreferenceUtil;

import org.angmarch.views.NiceSpinner;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class SettingActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    EditText serverEdit;
    EditText tokenEdit;
    EditText sdkIdEdit;
    EditText remarkEdit;
    EditText xTokenEdit;

    NiceSpinner niceSpinner;

    private void initView() {
        serverEdit = findViewById(R.id.serverEdit);
        tokenEdit = findViewById(R.id.tokenEdit);
        sdkIdEdit = findViewById(R.id.sdkIdEdit);
        remarkEdit = findViewById(R.id.remarkEdit);
        xTokenEdit = findViewById(R.id.xTokenEdit);
        niceSpinner = findViewById(R.id.mediaSpinner);

        List<String> dataset = new LinkedList<>(Arrays.asList("Rtp", "WebRtc"));
        niceSpinner.attachDataSource(dataset);

        String sipServer = (String) SharePreferenceUtil.getInstance().getValue(AttrsConstant.SIP_SERVER, "");
        String token = (String) SharePreferenceUtil.getInstance().getValue(AttrsConstant.TOKEN, "");
        String sdkId = (String) SharePreferenceUtil.getInstance().getValue(AttrsConstant.SDK_ID, "");
        String remark = (String) SharePreferenceUtil.getInstance().getValue(AttrsConstant.REMARK, "");
        String xToken = (String) SharePreferenceUtil.getInstance().getValue(AttrsConstant.X_TOKEN, "");
        int mediaType = (int) SharePreferenceUtil.getInstance().getValue(AttrsConstant.MEDIA_TYPE, 0);

        serverEdit.setText(sipServer);
        tokenEdit.setText(token);
        sdkIdEdit.setText(sdkId);
        remarkEdit.setText(remark);
        xTokenEdit.setText(xToken);
        niceSpinner.setSelectedIndex(mediaType);
    }

    public void onClick(View view) {
        SharePreferenceUtil.getInstance().setValue(AttrsConstant.SIP_SERVER, serverEdit.getText().toString());
        SharePreferenceUtil.getInstance().setValue(AttrsConstant.TOKEN, tokenEdit.getText().toString());
        SharePreferenceUtil.getInstance().setValue(AttrsConstant.SDK_ID, sdkIdEdit.getText().toString());
        SharePreferenceUtil.getInstance().setValue(AttrsConstant.REMARK, remarkEdit.getText().toString());
        SharePreferenceUtil.getInstance().setValue(AttrsConstant.X_TOKEN, xTokenEdit.getText().toString());
        SharePreferenceUtil.getInstance().setValue(AttrsConstant.MEDIA_TYPE, niceSpinner.getSelectedIndex());
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }
}
